//
//  YHEmotionPackage.swift
//  EmotionKeyboard
//
//  Created by 昱含 on 16/5/31.
//  Copyright © 2016年 YuHan. All rights reserved.
//

import UIKit
let EmotionCount = 21
let EmotionColCount = 7
let EmotionRowCount = 3

class YHEmotionPackage: NSObject {
    var name: String?
    lazy var sectionEmotion = [[YHEmotions]]()
    init(id: String, name: String, array: [[String:String]]) {
        self.name = name
        super.init()
        
        var tempArray = [YHEmotions]()
        var index = 0
        for item in array {
            index+=1
            let emotionModel = YHEmotions(id: id, dict: item)
            tempArray.append(emotionModel)
            if index == 20 {
                let delete = YHEmotions(isDelete: true)
                tempArray.append(delete)
                index = 0
                
            }
            
        }
        
        let delta = tempArray.count % EmotionCount
        if delta != 0 {
            for _ in delta..<20 {
                let space = YHEmotions(isEmpty: true)
                tempArray.append(space)
            }
            let delete = YHEmotions(isDelete: true)
            tempArray.append(delete)
            
        }
        
        sectionEmotion = sectionEmotions(tempArray)
    }
    
    private func sectionEmotions(array: [YHEmotions]) -> [[YHEmotions]]{
        let pageCount = (array.count - 1) / EmotionCount + 1
        var sectionArr = [[YHEmotions]]()
        for i in 0..<pageCount {
            let loc = i * EmotionCount
            var length = EmotionCount
            if loc + length > array.count {
                length = array.count - loc
            }
            let subArray = (array as NSArray).subarrayWithRange(NSRange(location: loc, length: length))
            sectionArr.append(subArray as! [YHEmotions])
            
        }
        
        return sectionArr
        
        
    }

}
